package org.goyo.main.services.pur.impl;

import java.util.List;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.goyo.common.baseBean.Constants;
import org.goyo.common.exception.DBException;
import org.goyo.common.exception.ServiceException;
import org.goyo.common.utils.DateUtils;
import org.goyo.common.utils.Pager;
import org.goyo.main.domain.dao.pur.IPurOrderDao;
import org.goyo.main.domain.entity.pur.PurOrder;
import org.goyo.main.domain.entity.pur.PurOrderEntry;
import org.goyo.main.domain.entity.sale.SaleOrder;
import org.goyo.main.domain.entity.sale.SaleOrderEntry;
import org.goyo.main.domain.entity.system.SysUser;
import org.goyo.main.services.BaseService;
import org.goyo.main.services.pur.IPurOrderService;
import org.goyo.main.services.system.IBillNumberService;
import org.springframework.stereotype.Service;
@Service
@Transactional
public class PurOrderServiceImpl extends BaseService implements IPurOrderService{
	@Resource
	private IPurOrderDao purOrderDao;
	@Override
	public Pager<PurOrder> findByCondition(String where, Pager pager,Object[] groupUuids)
			throws DBException, ServiceException {
		
		return  purOrderDao.findByCond(where, pager,groupUuids);
	}

	@Override
	public PurOrder saveOrUpdateInfo(PurOrder info, Object[] uuids,SysUser user)
			throws DBException, ServiceException {
		String uuid = null;
		//保存用户数据
		info.setStatus(Constants.SAVESTATUS);
		info.setAlterDate(DateUtils.getCurrentDate());
		info.setAlterUser(user);
		if(info.getUuid() == null || "".equals(info.getUuid())){
			info.setUuid(null);
			info.setCreateDate(DateUtils.getCurrentDate());
			info.setCreateUser(user);
			uuid = purOrderDao.save(info);
//			alterBillNumber(PurOrder.BIZTYPE, info.getNumber());
		}else{
			purOrderDao.update(info);
			uuid = info.getUuid();
		}
		//删除数据
		if(uuids != null && uuids[0] != null){
			purOrderDao.delBatch(PurOrderEntry.class, "uuid", uuids);
		}
		info.setUuid(uuid);
		return info;
	}

	@Override
	public List<PurOrderEntry> findByParentId(String parentId) throws DBException,
			ServiceException {
		return purOrderDao.findByParentId(parentId);
	}
	
	
	
	@Override
	public void delByUuids(Object[] uuids) throws DBException, ServiceException {
		//批量删除
		for(Object uuid : uuids){
			PurOrder info = purOrderDao.findByUuid(PurOrder.class, uuid.toString());
			purOrderDao.delete(info);
		}
	}

	@Override
	public PurOrder submitByUuids(Object[] uuids,SysUser user) throws DBException,
			ServiceException {
		PurOrder info = null;
		//批量提交
		for(Object uuid : uuids){
			info = purOrderDao.findByUuid(PurOrder.class, uuid.toString());
			info.setAlterDate(DateUtils.getCurrentDate());
			info.setAlterUser(user);
			info.setSubmitDate(DateUtils.getCurrentDate());
			info.setSubmitUser(user);
			info.setStatus(Constants.SUBMITTATUS);
			purOrderDao.update(info);
		}
		return info;
	}

	@Override
	public PurOrder auditByUuids(Object[] uuids,SysUser user) throws DBException,
			ServiceException {
		PurOrder info = null;
		//批量审核
		for(Object uuid : uuids){
			info = purOrderDao.findByUuid(PurOrder.class, uuid.toString());
			info.setAlterDate(DateUtils.getCurrentDate());
			info.setAlterUser(user);
			info.setAuditDate(DateUtils.getCurrentDate());
			info.setAuditUser(user);
			info.setStatus(Constants.AUDITSTATUS);
			purOrderDao.update(info);
		}
		return info;
	}

	@Override
	public PurOrder unAuditByUuids(Object[] uuids,SysUser user) throws DBException,
			ServiceException {
		PurOrder info = null;
		//批量反审核
		for(Object uuid : uuids){
			info = purOrderDao.findByUuid(PurOrder.class, uuid.toString());
			info.setAlterDate(DateUtils.getCurrentDate());
			info.setAlterUser(user);
			info.setAuditDate(null);
			info.setAuditUser(null);
			info.setSubmitUser(null);
			info.setSubmitDate(null);
			info.setStatus(Constants.SAVESTATUS);
			purOrderDao.update(info);
		}
		return info;
	}

	@Override
	public PurOrder findByUuuid(String uuid) throws DBException,
			ServiceException {
		PurOrder info = purOrderDao.get(PurOrder.class, uuid);
		return info;
	}
	
	@Override
	public Pager<SaleOrderEntry> findByEntryCondition(String where, Pager pager)
			throws DBException, ServiceException {
		pager = purOrderDao.findByEntryCond(where, pager);
//		List<PurOrderEntry> entrys = pager.getList();
//		if(entrys != null && entrys.size() > 0){
//			PurOrder info = purOrderDao.get(PurOrder.class, entrys.get(0).getParentId());
//			for(PurOrderEntry entry :entrys){
//				entry.setPurOrder(info);
//			}
//		}
		return pager;
	}
}
